﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Web.UI.WebControls;
using System.Data;


namespace Heyu.Utility
{
    /// <summary>
    /// 处理文件
    /// </summary>
    public class HandlerUtility
    {
        public HandlerUtility()
        {
            //1111111
        }

        /// <summary>
        /// 将一个类的值赋予另外一个类（同属性）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="O"></typeparam>
        /// <param name="t"></param>
        /// <param name="o"></param>
        public static void SetClassAttrFromOtherClass<T, O>(T t, O o)
        {
            foreach (var prop in typeof(O).GetProperties())
            {
                if (prop.CanRead && prop.CanWrite)
                {
                    prop.SetValue(t, prop.GetValue(o, null), null);
                }
            }
        }
        /// <summary>
        /// 把datarow 转为 class
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="dr"></param>
        public static void DataRowToModel<T>(T t, DataRow dr)
        {
            foreach (var prop in typeof(T).GetProperties())
            {
                if (dr.Table.Columns.Contains(prop.Name) && prop.CanRead && prop.CanWrite)
                {
                    object value = null;

                    if (!string.IsNullOrEmpty(dr[prop.Name].ToString()))
                    {
                        //如日期格式 System.Nullable<System.DateTime> (DateTime?)类型，而不是 System.DateTime 类型。
                        var propertyType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;//如果返回如：DateTime? int? decimal? bool? 则属性名改为DateTime int decimal bool
                        switch (propertyType.Name)
                        {
                            case "String":
                                value = dr[prop.Name].ToString();
                                break;
                            case "Boolean":
                                value = Convert.ToBoolean(dr[prop.Name]);
                                break;
                            case "Int32":
                                value = Convert.ToInt32(dr[prop.Name]);
                                break;
                            case "DateTime":
                                value = Convert.ToDateTime(dr[prop.Name]);
                                break;
                            case "Decimal":
                                value = Convert.ToDecimal(dr[prop.Name]);
                                break;
                            default:
                                value = null;
                                break;
                        }
                    }
                    //prop.SetValue(model, prop.GetValue(value, null), null);

                    prop.SetValue(t, value, null);
                }
            }
        }


        #region 控件值绑定

        public static void bindDropDownList(DropDownList ddl, DataTable dt, string text, string value, object firstText, object firstValue)
        {
            ddl.DataSource = dt;
            ddl.DataTextField = text;
            ddl.DataValueField = value;
            ddl.DataBind();
            if (firstText != null)
            {
                if (firstValue != null)
                {
                    ddl.Items.Insert(0, new ListItem(firstText.ToString(), firstValue.ToString()));
                }
                else
                {
                    ddl.Items.Insert(0, new ListItem(firstText.ToString()));
                }
            }
        }
        public static void bindDropDownList(DropDownList ddl, DataTable dt, string text, string value, object firstText)
        {
            bindDropDownList(ddl, dt, text, value, firstText, null);
        }
        public static void bindDropDownList(DropDownList ddl, DataTable dt, string text, string value)
        {
            bindDropDownList(ddl, dt, text, value, null, null);
        }
        public static void bindCheckBoxList(CheckBoxList cblist, DataTable dt, string text, string value)
        {
            cblist.DataSource = dt;
            cblist.DataTextField = text;
            cblist.DataValueField = value;
            cblist.DataBind();
        }
        public static void bindRadioButtonList(RadioButtonList rblist, DataTable dt, string text, string value)
        {
            rblist.DataSource = dt;
            rblist.DataTextField = text;
            rblist.DataValueField = value;
            rblist.DataBind();
        }

        #endregion


        /// <summary>
        /// 得到CheckBoxList中选中了的值
        /// </summary>
        /// <param name="checkList">CheckBoxList</param>
        /// <param name="separator">分割符号</param>
        /// <returns></returns>
        public static string GetChecked(CheckBoxList checkList, string separator)
        {
            string selval = "";
            for (int i = 0; i < checkList.Items.Count; i++)
            {
                if (checkList.Items[i].Selected)
                {
                    selval += checkList.Items[i].Value + separator;
                }
            }
            return selval;
        }
        /// <summary>
        /// 初始化CheckBoxList中哪些是选中了的        
        /// </summary>
        /// <param name="checkList">CheckBoxList</param>
        /// <param name="selval">选中了的值串例如："0,1,1,2,1"</param>
        /// <param name="separator">值串中使用的分割符例如"0,1,1,2,1"中的逗号</param>
        public static void SetChecked(CheckBoxList checkList, string selval, string separator)
        {
            selval = separator + selval + separator;        //例如："0,1,1,2,1"->",0,1,1,2,1,"
            for (int i = 0; i < checkList.Items.Count; i++)
            {
                checkList.Items[i].Selected = false;
                string val = separator + checkList.Items[i].Text.Trim() + separator;
                if (selval.IndexOf(val) != -1)
                {
                    checkList.Items[i].Selected = true;
                    selval = selval.Replace(val, separator);        //然后从原来的值串中删除已经选中了的
                    if (selval == separator)        //selval的最后一项也被选中的话，此时经过Replace后，只会剩下一个分隔符
                    {
                        selval += separator;        //添加一个分隔符
                    }
                }
            }
            //selval = selval.Substring(1, selval.Length - 2);        //除去前后加的分割符号
            //return selval;
        }
        public static void setAllChecked(CheckBoxList checkList)
        {
            for (int i = 0; i < checkList.Items.Count; i++)
            {
                checkList.Items[i].Selected = true;
            }
        }
        public static void setAllNoChecked(CheckBoxList checkList)
        {
            for (int i = 0; i < checkList.Items.Count; i++)
            {
                checkList.Items[i].Selected = false;
            }
        }
        public static bool checkChoose(string ids, int count, out string res)
        {
            bool flag = false;
            res = "";

            if (!string.IsNullOrEmpty(ids))
            {
                if (ids.Split(',').Length <= count)
                {
                    flag = true;
                }
                else
                {
                    res = "最大只能选择" + count + "条数据！";
                }
            }
            else
            {
                res = "请选择要操作的行！";
            }

            return flag;
        }


    }
}
